#!/usr/bin/env python
# import single_process.init
# from datetime import datetime
# from operator import itemgetter
# from ztz.db.sync.app import Q
# from parse import convert_pair_list
import re
from tzutil.extract import extract, extract_li
from tzutil.day import str2day, day_today
from tzutil.currency.const import CURRENCY_CN_SYMBOL, CURRENCY_SYMBOL_CN
from tzutil.req import requests
# from tz.db.pg import PG

DAY_TODAY = day_today()


def fetch_currency():
    """返回需要抓取的币种类别 ["美元", "港币"] """
    html = requests.get('http://fx.cmbchina.com/hq/').content.decode(
        "utf-8", "ignore"
    )
    all_currency = extract_li('<td class="fontbold">', '</td>', html)
    return all_currency


def fetch_page(currency, html):
    tr_list = extract_li('<tr>', '</tr>', html)

    li = []  # 日期和中间价tuple的数组

    for tr in tr_list:
        td_date = extract('<td align="center">', '</td>', tr)
        if td_date:
            td_middle_rate = list(
                extract_li('<td class="numberright">', '</td>', tr)
            )
            year, month, day = map(int, re.findall('\d+', td_date.strip()))
            day = str2day(td_date)
            li.append(
                (
                    day, "%.4f" % (
                        (float(td_middle_rate[0]) + float(td_middle_rate[2])
                         ) / 200.0
                    )
                )
            )
    return li


def fetch_currency_rate(currency, begin=0):
    currency = CURRENCY_SYMBOL_CN[currency]
    url = 'http://fx.cmbchina.com/Hq/History.aspx?nbr=%s&page='
    # currency_sign = CURRENCY_CN_SYMBOL[currency]
    html = requests.get(url % currency).content.decode("utf-8", "ignore")
    last_page = list(
        extract_li(
            '<a href="', 'class="text"',
            extract('<div class="function">', '<div', html)
        )
    )[-1]
    last_page_num = int(extract('page=', '"', last_page))
    li = []

    for page in range(1, last_page_num + 1):  # fetch all page
        to_url = url % currency + str(page)
        print(to_url)
        html = requests.get(to_url).content.decode("utf-8", "ignore")
        t = fetch_page(currency, html)
        for i in t:
            if i[0] <= begin:
                return li
            li.insert(0, i)
    return li


# o = PG.query_one(
#     'SELECT day FROM stock.currency_rate WHERE currency=%s ORDER by day DESC limit 1',
#     currency_sign
# )
# if not o:
#     begin_day = 0
# else:
#     begin_day = o.day

# print(DAY_TODAY, begin_day)

# if begin_day == DAY_TODAY:
#     return

# for day, rate in reversed(li):
#     PG.insert(
#         'stock.currency_rate',
#         dict(
#             currency=currency_sign,
#             day=day,
#             rate=rate,
#         ),
#         on_conflict="(currency,day) DO UPDATE SET rate=%s" % rate
#     )

# if begin_day == 0 and 'USD' == currency_sign:
#     from init import init, INIT_CURRENCY
#     init()

# def gen_usd_hkd():
#     """从HKD/CNY和USD/CNY生成USD/HKD, 日期有可能不一样，归并两个逆序数组
#     取共有的日期计算汇率"""
#     hkd_cny_history = Q.rate.find_one(pair="HKD/CNY").history
#     usd_cny_history = Q.rate.find_one(pair="USD/CNY").history

#     hkd_cny_dict = {i[0]: i[1] for i in hkd_cny_history}
#     usd_cny_dict = {i[0]: i[1] for i in usd_cny_history}
#     common_date_set = set(
#         list(
#             hkd_cny_dict.keys())) & set(
#         list(
#             usd_cny_dict.keys()))

#     usd_hkd_history = []

#     for day in sorted(common_date_set):
#         usd_hkd_history.append(
#             tuple([day, usd_cny_dict[day] / hkd_cny_dict[day]]))

#     Q.rate.upsert(pair="USD/HKD")(history=usd_hkd_history)


def main():
    currency_list = fetch_currency()
    for currency in currency_list:
        print(CURRENCY_CN_SYMBOL[currency])
        fetch_currency_rate(currency)


if __name__ == '__main__':
    main()
    # gen_usd_hkd()

    # print(fetch_currency())
